/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Detection.java
 *
 * Created on May 28, 2013, 3:30:10 PM
 */

package cofud;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Rendra
 */
public class Detection extends javax.swing.JInternalFrame {
    
    /** Creates new form HomePage */
    public Detection() {
        initComponents();
        try {
            parseSourceFile();
        } catch (Exception ex) {
            Logger.getLogger(Detection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        commentsPane = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        functionsPane = new javax.swing.JTextPane();
        jLabel2 = new javax.swing.JLabel();
        elapsedTime = new javax.swing.JLabel();
        lbl_nama_file = new javax.swing.JLabel();

        commentsPane.setEditable(false);
        commentsPane.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        jScrollPane2.setViewportView(commentsPane);

        jLabel1.setText("Comments Section");

        functionsPane.setEditable(false);
        functionsPane.setContentType("text/html"); // NOI18N
        functionsPane.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        jScrollPane3.setViewportView(functionsPane);

        jLabel2.setText("Functions Section");

        elapsedTime.setText("Elapsed time : ");

        lbl_nama_file.setText("lbl_nama_file");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(440, 440, 440)
                                .addComponent(jLabel2)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(elapsedTime)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lbl_nama_file)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(elapsedTime)
                    .addComponent(lbl_nama_file))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void parseSourceFile() throws Exception{
        commentsPane.setContentType("text/html");
        functionsPane.setContentType("text/html");
        commentsPane.setText("");
        functionsPane.setText("");
        
        StringBuilder sb_cm = new StringBuilder();
        StringBuilder sb_fn = new StringBuilder();
        
        long startTime = System.currentTimeMillis();
        
        String sourceFile = MyProgram.myBridge.getSourceFile();
        String parsed_srcfile = MyProgram.myBridge.readFile(sourceFile);
        
        //String regex_comments = "(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)";
        //String regex_comments = "/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/";
        //String regex_comments = "(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)";
        
        /** Update Regex Comment 25 Juni 2013 */
        String regex_comments = "/\\*(?>(?:(?>[^*]+)|\\*(?!/))*)\\*/|(//.*)";
        String regex_function = "\\w+ +\\w+ *\\([^\\)]*\\) *\\{";
        String regex_empty_ln = "^\\s*$";
        
        int total_empty_lines = 0;
        int total_effectiveln = 0;
        int total_comments_ln = 0;
        int total_linesofcode = MyProgram.myBridge.getTotalLines();
        int [] total_partcode = new int[4];
        
        Pattern p_c = Pattern.compile(regex_comments, Pattern.MULTILINE);
        Matcher m_c = p_c.matcher(parsed_srcfile);
        while (m_c.find()) {
            //total_comments_ln++;
            total_comments_ln += getCommentLines(m_c.group());
            sb_cm.append("<p>").append(m_c.group()).append("</p>").append("\n");
        }
        
        Pattern p_s = Pattern.compile(regex_function, Pattern.MULTILINE);
        Matcher m_s = p_s.matcher(parsed_srcfile);
        while (m_s.find()){
            sb_fn.append("<p>").append(m_s.group()).append("}").append("</p>").append("\n");
        }
        
        Pattern p_t = Pattern.compile(regex_empty_ln, Pattern.MULTILINE);
        Matcher m_t = p_t.matcher(parsed_srcfile);
        while (m_t.find()){
            total_empty_lines++;
        }
        
        total_effectiveln = total_linesofcode - total_empty_lines;
        long stopTime = System.currentTimeMillis();
        float elapsedTimeSec = ((startTime-stopTime)/1000F)*(-1);
        
        total_partcode[0] = total_linesofcode;
        total_partcode[1] = total_empty_lines;
        total_partcode[2] = total_comments_ln;
        total_partcode[3] = total_effectiveln;
        MyProgram.myBridge.setPartCode(total_partcode);
        MyProgram.myBridge.setCommentsOfCode(sb_cm);
        MyProgram.myBridge.setFunctionOfCode(sb_fn);
        
        commentsPane.setText(sb_cm.toString());
        functionsPane.setText(sb_fn.toString());
        //System.out.println(sb_cm.toString());
        elapsedTime.setText("Elapsed time : "+Float.toString(elapsedTimeSec)+" sec.");
        lbl_nama_file.setText("Processed file : "+MyProgram.myBridge.getSourceFile());
    }
    
    private int getCommentLines(String comment){
        comment = Pattern.compile("\\s*$", Pattern.MULTILINE).matcher(comment).replaceAll("");
        return comment.split("\n").length;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane commentsPane;
    private javax.swing.JLabel elapsedTime;
    private javax.swing.JTextPane functionsPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbl_nama_file;
    // End of variables declaration//GEN-END:variables

}
